import { http } from '@/utils/request'
import type { 
  LoginForm, 
  RegisterForm, 
  AuthResponse, 
  ApiResponse,
  User 
} from '@/types'

/**
 * 认证API
 */
export const authApi = {
  // 用户登录
  login(data: LoginForm): Promise<ApiResponse<AuthResponse>> {
    return http.post('/v1/auth/login', data)
  },

  // 用户注册
  register(data: RegisterForm): Promise<ApiResponse<AuthResponse>> {
    return http.post('/v1/auth/register', data)
  },

  // 用户登出
  logout(): Promise<ApiResponse<void>> {
    return http.post('/v1/auth/logout')
  },

  // 发送验证码
  sendVerificationCode(data: {
    email: string
    type?: 'register' | 'reset_password' | 'change_email'
  }): Promise<ApiResponse<{ expires_in: number }>> {
    return http.post('/v1/auth/send-verification-code', data)
  },

  // 重置密码
  resetPassword(data: {
    token: string
    newPassword: string
  }): Promise<ApiResponse<void>> {
    return http.post('/v1/auth/reset-password', data)
  },

  // 修改密码
  changePassword(data: {
    old_password: string
    new_password: string
  }): Promise<ApiResponse<void>> {
    return http.post('/v1/auth/change-password', data)
  },

  // 检查登录状态
  checkLogin(): Promise<ApiResponse<{
    is_logged_in: boolean
    user_id?: number
    username?: string
    email?: string
    avatar_url?: string
    role?: string
  }>> {
    return http.get('/check_login')
  },

  // 刷新Token
  refreshToken(): Promise<ApiResponse<{ token: string }>> {
    return http.post('/v1/auth/refresh-token')
  },

  // 验证Token
  verifyToken(): Promise<ApiResponse<User>> {
    return http.get('/v1/auth/verify-token')
  }
}

